package com.adtech.adms.device.service.impl;

import java.util.Date;
import java.util.List;

import org.slstudio.acs.hms.bootstrap.IBootStrapStrategy;
import org.slstudio.acs.hms.bootstrap.bean.BootStrapBean;

import com.adtech.adms.commons.util.IPRange;
import com.adtech.adms.device.DeviceConstants;
import com.adtech.adms.device.pojo.GuiDevice;
import com.adtech.adms.device.pojo.GuiPolicy;
import com.adtech.adms.device.service.PolicyService;
import com.adtech.adms.system.SystemConstants;
import com.adtech.adms.system.pojo.Topo;
import com.adtech.adms.system.pojo.User;
import com.adtech.core.dao.HibernateDAO;
import com.adtech.core.dao.exp.Expression;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.QueryConstants;
import com.adtech.core.dao.query.QueryCriterions;
import com.adtech.core.dao.query.Sort;

public class PolicyServiceImpl implements PolicyService, IBootStrapStrategy {
	private HibernateDAO dao;

	public void setDao(HibernateDAO dao) {
		this.dao = dao;
	}

	@Override
	public List<GuiPolicy> getPolicyList(Pager pager, Expression expression,
			Sort sort) {
		// TODO Auto-generated method stub
		if (sort == null || sort.isEmpty()) {
			sort = new Sort();
			sort.addSort(DeviceConstants.Device.ID, QueryConstants.DESC);
		}
		return (List<GuiPolicy>) dao.getByQuery(GuiPolicy.class, pager,
				expression, sort);
	}

	@Override
	public void save(GuiPolicy policy) {
		// GuiPolicy existPolicy = dao.getById(GuiPolicy.class, policy.getId());
		if (policy.getId() == null) {
			if (policy.getTopoIds() != null
					&& policy.getTopoIds().length() != 0) {
				String[] ids = policy.getTopoIds().split(",");

				for (int i = 0; i < ids.length; i++) {
					if (!ids[i].equals("")) {
						Topo topo = dao.loadById(Topo.class, new Long(ids[i]));
						topo.setPolicy(policy);
						policy.getTopos().add(topo);
					}
				}
			}
			policy.setEditTime(new Date());
			policy.setCreateTime(new Date());
			dao.add(policy);
			return;
		}

		GuiPolicy existPolicy = (GuiPolicy) dao.loadById(GuiPolicy.class,
				policy.getId());
		mergePolicy(existPolicy, policy);
		
		
		dao.saveOrUpdate(existPolicy);
	}

	private void mergePolicy(GuiPolicy existPolicy, GuiPolicy policy) {
		existPolicy.setSafeGateway1(policy.getSafeGateway1());
		existPolicy.setSafeGateway2(policy.getSafeGateway2());
		existPolicy.setSafeGateway3(policy.getSafeGateway3());
		existPolicy.setGateway1(policy.getGateway1());
		existPolicy.setGateway2(policy.getGateway2());
		existPolicy.setGateway3(policy.getGateway3());
		existPolicy.setGatewayPort(policy.getGatewayPort());
		existPolicy.setAcsAccount(policy.getAcsAccount());
		existPolicy.setAcsAddress(policy.getAcsAddress());
		existPolicy.setAcsPassword(policy.getAcsPassword());
		existPolicy.setTopoIds(policy.getTopoIds());
		existPolicy.setIpRange(policy.getIpRange());
		existPolicy.setEditTime(new Date());
		String[] ids = policy.getTopoIds().split(",");

		existPolicy.getTopos().clear();
		for (int i = 0; i < ids.length; i++) {
			if (!ids[i].equals("")) {
				Topo topo = dao.loadById(Topo.class, new Long(ids[i]));
				topo.setPolicy(existPolicy);
				existPolicy.getTopos().add(topo);
			}
		}
	}

	@Override
	public GuiPolicy getPolicy(Integer id) {
		GuiPolicy policy = dao.getById(GuiPolicy.class, id);
		return policy;
	}

	@Override
	public void delete(String id) {
		if (id == null || id.length() == 0) {
			return;
		}
		String[] ids = id.split(",");
		for (String policyId : ids) {
			if (policyId != null && policyId.length() > 0) {
				GuiPolicy policy = dao.getById(GuiPolicy.class, Integer
						.parseInt(policyId));
				List<Topo> topos = policy.getTopos();
				for (Topo topo : topos) {
					topo.setPolicy(null);
				}
				dao.delete(policy);
			}
		}
		dao.flushSession();
	}

	@Override
	public BootStrapBean getBootstrapConfig(String deviceKey, String ip) {
		QueryCriterions hc = new QueryCriterions();
		hc.and(SystemConstants.Device.KEY, deviceKey, QueryConstants.EQ);
		GuiDevice device = dao
				.getOneByQuery(GuiDevice.class, hc.toExpression());
		if (device == null) {
			return null;
		}
		List<GuiPolicy> policys = dao.getByQuery(GuiPolicy.class, null);
		GuiPolicy policy = null;
		if (!("").equals(ip)) {
			for (GuiPolicy guiPolicy : policys) {
				if (guiPolicy.getIpRange() != null
						&& !"".equals(guiPolicy.getIpRange())) {
					if (IPRange.ipIsValid(guiPolicy.getIpRange(), ip, ",")) {
						policy = guiPolicy;
					}
				}
			}
		}
		BootStrapBean bean = new BootStrapBean();
		if (policy != null) {
			setPolicy(policy, bean);
			return bean;
		} else {
			hc.clear();
			hc.and("nodeid", String.valueOf( device.getTopoSid()), QueryConstants.EQ);
			Topo topo = dao.getOneByQuery(Topo.class, hc.toExpression());
			if (topo != null && topo.getPolicy() != null) {
				setPolicy(topo.getPolicy(), bean);
				return bean;
			}
		}

		return null;
	}

	private void setPolicy(GuiPolicy policy, BootStrapBean bean) {
		bean.setAcsAccount(policy.getAcsAccount());
		bean.setAcsAddress(policy.getAcsAddress());
		bean.setAcsPassword(policy.getAcsPassword());
		bean.setGateway1(policy.getGateway1());
		bean.setGateway2(policy.getGateway2());
		bean.setGateway3(policy.getGateway3());
		bean.setGatewayPort(policy.getGatewayPort());
		bean.setSafeGateway1(policy.getSafeGateway1());
		bean.setSafeGateway2(policy.getSafeGateway2());
		bean.setSafeGateway3(policy.getSafeGateway3());
	}
}
